#include "data_define.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_tls_errors.h"
#include <stdint.h>
#include <string.h>

#if 1
#include "net_task.h"
#include "pack_make_parsing.h"
#include "queue_wrapper.h"
#include "rtc.h"
#include "sht30_task.h"

#endif

enum enum_register_state {
  REGISTER_NO,
  REGISTER_doing,
  REGISTER_YES
} register_state = REGISTER_NO;
uint32_t last_time = 0;

void app_main(void) {
  nvs_init();
  uint8_t id[6];
  cl_rtc_init();
  esp_base_mac_addr_get(id);
  sprintf(g_mac_str, MACSTR, MAC2STR(id));

  load_config_nvs(&g_cfg);
  ESP_LOGI("main", "Hello world!");
  ESP_LOGI("main", "wifi_ssid:%s", g_cfg.wifi_ssid);
  ESP_LOGI("main", "wifi_pass:%s", g_cfg.wifi_pass);
  ESP_LOGI("main", "tcp_server_ip:%s", g_cfg.tcp_server_ip);
  ESP_LOGI("main", "tcp_server_port:%d", g_cfg.tcp_server_port);
  ESP_LOGI("main", "temp_k:%f", g_cfg.temp_k);
  ESP_LOGI("main", "temp_b:%f", g_cfg.temp_b);
  ESP_LOGI("main", "humi_k:%f", g_cfg.humi_k);
  ESP_LOGI("main", "humi_b:%f", g_cfg.humi_b);
  ESP_LOGI("main", "collect_interval:%" PRId32, g_cfg.collect_interval);
  ESP_LOGI("main", "upload_interval:%" PRId32, g_cfg.upload_interval);
  strcpy(g_cfg.tcp_server_ip, "192.168.0.110");
  // g_cfg.tcp_server_port = 1234;
  gpio_config_t io_conf = {};
  io_conf.intr_type = GPIO_INTR_DISABLE;
  io_conf.mode = GPIO_MODE_OUTPUT;
  io_conf.pin_bit_mask = LED1;
  io_conf.pull_down_en = 0;
  io_conf.pull_up_en = 0;
  gpio_config(&io_conf);
  gpio_set_direction(LED1, GPIO_MODE_OUTPUT);

  queue_init();
  create_sht30_task();
  create_net_task();

  for (uint32_t main_cnt = 0;; main_cnt++) {

    gpio_set_level(LED1, main_cnt % 2);
    vTaskDelay(pdMS_TO_TICKS(3 * 1000));
    float H, T;
    get_HT(&H, &T);

    char b[512];
    if (get_net_state() == S_TCP_TX_RX) {
      if (register_state == REGISTER_NO) {
        /* TODO 发送注册包 */
        make_register_pack(b, &g_cfg);
        b[strlen(b)] = '\n';
        queue_tx_push(b);
        register_state = REGISTER_doing;
      } else if (register_state == REGISTER_doing) {
        /* TODO 接受内容 解析确定是否完成注册 */
        if (queue_rx_pop(b, 512)) {

          parse_result_e rst = parse_regiser_ack_pack(b);
          if (rst == PARSE_OK) {
            ESP_LOGI("main", "parse_json_pack() ok");
            register_state = REGISTER_YES;
            last_time = xTaskGetTickCount();
          }
        }
      } else if (register_state == REGISTER_YES) {
        /* 判定上传间隔,到达则立即上传内容 */
        if ((xTaskGetTickCount() - last_time) > 3000) {
          last_time = xTaskGetTickCount();
          make_realtime_pack(b, H, T, rtc_read_timestamp());
        }
        /* 接受内容,确定时间同步或参数写入 */
        if (queue_rx_pop(b, 512)) {
          parse_json_pack(b, &g_cfg);
        }
      }

    } else {
      register_state = REGISTER_NO;
    }

    // xQueueSend(net_send_task_queue, "hello", portMAX_DELAY);

    // if (xQueueReceive(net_send_task_queue, recv_buf, 0) == pdPASS) {
    //   ESP_LOGI("main", "recv_buf:%s", recv_buf);
    // }
  }
}
